<script src="../../../../public/assets/js/require-backend.js"></script>
<form id="add-form" class="form-horizontal" method="POST" action="{:url('ksave')}">
    <div class="form-group">
        <label class="control-label col-xs-12 col-sm-2">名称:</label>
        <div class="col-xs-12 col-sm-8">
            <input type="text" name="name" value="{$data['name']|default=''}" class="form-control"/>
        </div>
    </div>
    <div class="form-group">
        <label class="control-label col-xs-12 col-sm-2">开发模式:</label>
        <div class="col-xs-12 col-sm-8" id="cate_select">
            <label style="font-weight: normal;margin-right: 15px;cursor: pointer;"><input style="position: relative;top: 2px;" name="develop_model" value="1" 
                {if condition="(!empty($data)) AND $data['develop_model'] == 1"}checked{/if} type="radio">系统服务</label>
            <label style="font-weight: normal;margin-right: 15px;cursor: pointer;"><input style="position: relative;top: 2px;" name="develop_model" value="2"
                 {if condition="(empty($data)) OR $data['develop_model'] == 2"}checked{/if} type="radio">单一服务</label>
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-xs-12 col-sm-2">所属部分:</label>
        <div class="col-xs-12 col-sm-8">
            <label style="font-weight: normal;margin-right: 15px;cursor: pointer;">
                <input style="position: relative;top: 2px;" name="type" value="1"
                       {if condition="(!empty($data)) AND $data['type'] == 1"}checked{/if} type="radio">独立部分</label>
            <label style="font-weight: normal;margin-right: 15px;cursor: pointer;">
                <input style="position: relative;top: 2px;" name="type" value="2"
                       {if condition="(!empty($data)) AND $data['type'] == 2"}checked{/if} type="radio">通用部分</label>
        </div>
    </div>

    <div class="form-group price">
        <label class="control-label col-xs-12 col-sm-2">应用版本:</label>
        <div class="col-xs-12 col-sm-8">
           <div class="fixed-table-body">
                <table id="table" class="table table-striped table-bordered table-hover" width="100%" style="background-color: #fff;">
                    <thead>
                        <tr>
                            <th style="text-align: center; vertical-align: middle; ">
                                <div class="th-inner ">应用版本</div>
                                <div class="fht-cell"></div>
                            </th>
                            <th style="text-align: center; vertical-align: middle; ">
                                <div class="th-inner ">价格</div>
                                <div class="fht-cell"></div>
                            </th>
                        </tr>
                    </thead>
                    <tbody data-listidx="0" id="app_version">
                       {volist name="versions" id="vo"} 
                         <tr data-index="{$key}">
                            <td style="text-align: center; vertical-align: middle;">
                                <label><input  value="{$vo.id}" {if condition="!empty($list[$vo['id']])"}checked{/if}  type="checkbox">{$vo.name}</label>
                            </td>
                            <td style="text-align: center; vertical-align: middle;">
                                <input type="text" value="{$list[$vo['id']]|default=''}"  class="form-control" id="price_{$vo.id}"/>
                            </td>
                         </tr>
                        {/volist} 
                      
                    </tbody>
                </table>
            </div>
        </div>
     </div>

     <div class="form-group">
        <label class="control-label col-xs-12 col-sm-2"></label>
        <div class="col-xs-12 col-sm-8">
            <label><input value="0" type="radio" {if condition="!empty($list[0])"}checked {/if} id="price_radio">无版本</label>
            <input type="text" class="form-control" value="{$list[0]|default=''}" id="price_0"/>
        </div>
       
    </div>
   
    <div class="form-group">
        <label class="control-label col-xs-12 col-sm-2">应用范畴:</label>
        <div class="col-xs-12 col-sm-8 cate cate_1" id="cate_1" {if condition="(empty($data)) OR $data['develop_model'] != 1"}style="display: none;" {/if}>
           {volist name="list1" id="vo"}  
            <label style="font-weight: normal;margin-right: 15px;cursor: pointer;"><input style="position: relative;top: 2px;" name="cate_id" type="radio" {if condition="(!empty($data)) AND $data['cate_id'] == $vo['id']"}checked{/if} value="{$vo.id}">{$vo.name}</label>
           {/volist}  
        </div>
         <div class="col-xs-12 col-sm-8 cate cate_2" id="cate_2" {if condition="(!empty($data)) AND $data['develop_model'] != 2"}style="display: none;" {/if}>
           {volist name="list2" id="vo"}  
            <label style="font-weight: normal;margin-right: 15px;cursor: pointer;"><input style="position: relative;top: 2px;" name="cate_id" type="radio" {if condition="(!empty($data)) AND $data['cate_id'] == $vo['id']"}checked{/if} value="{$vo.id}">{$vo.name}</label>
           {/volist}  
        </div>
    </div>
    <div class="form-group">
        <label class="control-label col-xs-12 col-sm-2">单位:</label>
        <div class="col-xs-12 col-sm-8">
            <input type="text" name="company" value="{$data['company']|default=''}" class="form-control"/>
        </div>
    </div>
    <div class="form-group">
        <label class="control-label col-xs-12 col-sm-2">描述:</label>
        <div class="col-xs-12 col-sm-8">
            <textarea type="text" name="des" id="des" class="form-control">{$data['des']|default=''}</textarea>
        </div>
    </div>
    <div class="form-group">
        <label class="control-label col-xs-12 col-sm-2"></label>
        <div class="col-xs-12 col-sm-8">
            <input type="hidden" name="id" value="{$data['id']|default=''}" class="form-control"/>
            <button type="submit" id="submit" class="btn btn-success btn-embossed">{:__('OK')}</button>
        </div>
    </div>
</form>

<script>
    $(function(){
        $("#cate_select").find(":radio").click(function(){
            var cate_id=$(this).val();
            $(".cate").hide();
            $(".cate_"+cate_id).css("display","block");
        })
        $("#submit").click(function(){
             var items = new Array();
            if($("#price_radio").is(":checked")){
                var version=0;
                var price=$("#price_0").val();
                items.push([version,price]);
            }else{
                 var cheklist=$("#app_version").find(":checked");
                 for (var i = 0; i < cheklist.length; i++) {
                    var version=$(cheklist[i]).val();
                    var price=$("#price_"+version).val();
                    var item=[version,price];
                    items.push(item);
                 }
            }
            var version_price=JSON.stringify( items );
         
           
           var name=$("input[name=name]").val();
           var develop_model=$("input[name=develop_model]:checked").val();
           var cate_id=$("input[name=cate_id]:checked").val();
           var company=$("input[name=company]").val();
           var des=$("#des").val();
           var type=$("input[name=type]:checked").val();
           var id=$("input[name=id]").val();
           if(name==''){
               alert("请填写名称");
               return false;
           }
           if(develop_model==2 && type==undefined){
               alert("请选择所属部分");
               return false;
           }
           $.ajax({
                    type:'POST',
                    data:{
                        version_price:version_price,
                        name:name,
                        develop_model:develop_model,
                        cate_id:cate_id,
                        company:company,
                        des:des,
                        type:type,
                        id:id
                    },
                    url:"{:url('ksave')}",
                    success:function(data){                            
                          if(data.code==1){
                            alert(data.msg);
                            window.location.href=data.url;
                          }else{
                            alert(data.msg);
                          }
                    }
            });   
            return false;        
        });

    })
</script>